﻿using Kimd.Common;
using Kimd.Communication;
using Kimd.Logic;
using Kimd.Machine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WorkStation.Common;

namespace WorkStation.Logic
{
    public class UnloadPLCStatusLogic : FlowBase
    {
        private PLCOperator _plc;
        private short[] vals;
        public UnloadPLCStatusLogic(string flowName, FlowManager flowmanager) 
            : base(flowName, flowmanager)
        {
            _plc = MachineResource.Instance.MachinePLC["轨道PLC"];
        }

        public override void Calibrate()
        {
            base.Calibrate();
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override void Home()
        {
            base.Home();
        }

        public override void Init(out string errInfo)
        {
            base.Init(out errInfo);
        }

        public override void Method()
        {
            //if (!CheckFlowCondition()) return;

            if(GlobalVariable.PCReadPLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCInFlag>("Boat请求扫码")])
            {
                //
                Thread.Sleep(40);

                //lock (GlobalVariable.ConveyorPLCLock)
                //{
                //    while (!ReadPLC(_plc, RegisterType.D, 400, 25, out vals))
                //    {
                //        Thread.Sleep(40);
                //    }
                //}
                GlobalVariable.ConveyorPLC.ReadShort("400", 25, out vals);
                if (vals != null)
                {
                    GlobalVariable.CarrierCheckSN = CommonMethod.ConvertShortArrayToString(vals);
                }

                if (SystemConfig.Instance.MesParam.EnableMES && !CommonMethod.DoCarrierCheck(GlobalVariable.CarrierCheckSN, out string errInfo))
                {
                    GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("CarrierCheck失败")] = true;
                    Thread.Sleep(1000);
                    GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("CarrierCheck失败")] = false;
                    ShowWarnAndPause($"MES CarrierCheck失败!{errInfo}", "系统");
                    if (!CheckFlowCondition()) return;
                }
                else
                {
                    GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("CarrierCheck成功")] = true;
                    Thread.Sleep(1000);
                    GlobalVariable.PCWritePLCData[(int)EnumHelper.GetEnumValueFromDescription<PLCOutFlag>("CarrierCheck成功")] = false;
                }
            }
            System.Threading.Thread.Sleep(100);
        }

        public override void OnStop()
        {
            base.OnStop();
        }

        public override bool PreStartCheck()
        {
            return base.PreStartCheck();
        }

        public override void SpotCheck()
        {
            base.SpotCheck();
        }

        public override string ToString()
        {
            return base.ToString();
        }
    }
}
